package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.*;
import cn.itsource.order.domain.PetAcquisitionOrder;
import cn.itsource.order.mapper.PetAcquisitionOrderMapper;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.pet.service.IPetService;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long loginId) {

        //1消息要分配给最近店铺
        //1.1 获取最近店铺
        Point currentDistance = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(currentDistance, shopMapper.loadAll());
        //1.2 把店铺绑定到消息
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        //1.3 用户绑定
        User user =userMapper.loadByLoginInfoId(loginId);
        searchMasterMsg.setUser(user);
        searchMasterMsg.setUser_id(user.getId());
        //1.4 对象消息进行持久化
        searchMasterMsgMapper.save(searchMasterMsg);
        //2 发一个对象消息门店管理员
        Long adminId = nearestShop.getAdmin_id();
        Employee admin = employeeMapper.loadById(adminId);
        //SmsUtil.sendMsg(admin.getPhone(),"老板，来["+searchMasterMsg.getTitle()+"]生意了！赶快去接客！");
        System.out.println(admin.getPhone()+"老板，来["+searchMasterMsg.getTitle()+"]生意了！赶快去接客！");


    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long loginInfoId) {
        query.setState(0);
        //判断是否是店铺管管理员或员工-通过Logininfo_id查询Employee，如果有shopId就是店铺管理员或员工
        //否则就是平台管理员
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        if (employee.getShop_id()!=null)
            query.setShopId(employee.getShop_id());
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long loginInfoId) {
        query.setState(1);
        //判断是否是店铺管管理员或员工-通过Logininfo_id查询Employee，如果有shopId就是店铺管理员或员工
        //否则就是平台管理员
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        if (employee.getShop_id()!=null)
            query.setShopId(employee.getShop_id());
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> query(SearchMasterMsgQuery query, Long loginInfoId) {
        User user = userMapper.loadByLoginInfoId(loginInfoId);
        query.setUserId(user.getId());
        return super.queryPage(query);
    }


    @Autowired
    private IPetService petService;
    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;
    @Override
    public void handle(Pet pet,Long loginInfoId) {
        //1 修改修改状态
        Long searchMasterMsgId = pet.getSearch_master_msg_id();
        //通过id修改状态为已处理
        searchMasterMsgMapper.updateStateForHandledById(searchMasterMsgId);
        //2 创建宠物及详情 service去做
        petService.add(pet);

        //3创建订单
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfoId);
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(searchMasterMsgId);
        PetAcquisitionOrder petAcquisitionOrder = pet2order(pet, msg, employee.getId());
        petAcquisitionOrderMapper.save(petAcquisitionOrder);

        //4 @TODO 如果要收钱，创建支付单并付完款
        if (petAcquisitionOrder.getPrice().longValue()>0){
            System.out.println("你已经付款了。。。。。");
        }

    }
    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg msg,Long employeeId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(msg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(msg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(msg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(employeeId);
        return order;
    }
}
